//
//  AppDelegate.m
//  BaseRACDemo
//
//  Created by 庄波 on 2018/3/6.
//  Copyright © 2018年 庄波. All rights reserved.
//

#import "ZBAppDelegate.h"
#import "ZBHomePageViewModel.h"
#import "ZBLoginViewController.h"
#import "ZBLoginViewModel.h"
//语音
#import "iflyMSC/IFlySpeechUtility.h"

@interface ZBAppDelegate ()

/// APP管理的导航栏的堆栈
@property (nonatomic, readwrite, strong) ZBNavigationControllerStack *navigationControllerStack;
/// APP的服务层
@property (nonatomic, readwrite, strong) ZBViewModelServicesImpl *services;

@end

@implementation ZBAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    /// 初始化UI之前配置
    [self configureApplication:application initialParamsBeforeInitUI:launchOptions];
    // Config Service
    self.services = [[ZBViewModelServicesImpl alloc] init];
    // Config Nav Stack
    self.navigationControllerStack = [[ZBNavigationControllerStack alloc] initWithServices:self.services];
    // Configure Window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    // 重置rootViewController
    [self.services resetRootViewModel:[self _createInitialViewModel]];
    // 让窗口可见
    [self.window makeKeyAndVisible];
    
    // 保存版本号
    [[NSUserDefaults standardUserDefaults] setValue:K_APP_VERSION forKey:KApplicationVersionKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return YES;
}

#pragma mark - 在初始化UI之前配置
- (void)configureApplication:(UIApplication *)application initialParamsBeforeInitUI:(NSDictionary *)launchOptions
{
    /// 显示状态栏
    [application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    
    /// 配置键盘
    [self _configureKeyboardManager];
    
    /// 配置文件夹
    [self _configureApplicationDirectory];
    
    /// 配置数据库
    
    ///注册语音
    //语音
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"57d770ae"];
    [IFlySpeechUtility createUtility:initString];
    
    ///切换根控制器的通知
    @weakify(self);
    /// 监听切换根控制器的通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:MHRootViewControllerNotification object:nil] subscribeNext:^(NSNotification * note) {
        /// 这里切换根控制器
        @strongify(self);
        /// 切换根控制器
        [self.services resetRootViewModel:[self _createInitialViewModel]];
    }];


}             

#pragma mark - 创建根控制器
- (ZBViewModel *)_createInitialViewModel {
    // The user has logged-in.
//    NSString *version = [[NSUserDefaults standardUserDefaults] valueForKey:KApplicationVersionKey];
//    /// 版本不一样就先走 新特性界面
//    if (![version isEqualToString:K_APP_VERSION]){
//        return [[ZBHomePageViewModel alloc] initWithServices:self.services params:nil];
//
//    }else{
//        /// 这里判断一下
//        if (self.services.client.isLogin) {
//            /// 有账号+有用户数据
//            /// 已经登录，跳转到主页
//            return [[ZBHomePageViewModel alloc] initWithServices:self.services params:nil];
//        }else if(1){ /// 没账号+有用户数据
//            /// 跳转到账户登录页面
//            return [[ZBLoginViewModel alloc] initWithServices:self.services params:nil];
//        }else{
//            /// 第一次使用微信
//           // return [[MHBootLoginViewModel alloc] initWithServices:self.services params:nil];
//        }
//    }
    
    /// 这里判断一下
    if (self.services.client.isLogin) {
        /// 有账号+有用户数据
        /// 已经登录，跳转到主页
        return [[ZBHomePageViewModel alloc] initWithServices:self.services params:nil];
    }else if(1){ /// 没账号+有用户数据
        /// 跳转到账户登录页面
        return [[ZBLoginViewModel alloc] initWithServices:self.services params:nil];
    }else{
        /// 第一次使用微信
        // return [[MHBootLoginViewModel alloc] initWithServices:self.services params:nil];
    }
    return nil;
}

#pragma mark- 获取appDelegate
+ (ZBAppDelegate *)sharedDelegate{
    return (ZBAppDelegate *)[[UIApplication sharedApplication] delegate];
}


/// 配置键盘管理器
- (void)_configureKeyboardManager {
    IQKeyboardManager.sharedManager.enable = YES;
    IQKeyboardManager.sharedManager.enableAutoToolbar = NO;
    IQKeyboardManager.sharedManager.shouldResignOnTouchOutside = YES;
}

/// 配置文件夹
- (void)_configureApplicationDirectory
{

}





















- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
